package com.azt.provider.impl;

import com.azt.api.pojo.LevelConfig;
import com.azt.api.pojo.LevelUser;
import com.azt.api.pojo.req.LevelConfigReq;
import com.azt.api.pojo.vo.LevelConfigVO;
import com.azt.api.service.LevelConfigService;
import com.azt.enums.LevelTypeEnum;
import com.azt.provider.mapper.LevelConfigMapper;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.azt.utils.Preconditions.checkNotNull;

/**
 * 规则管理
 * Created by LiQZ on 2017/4/13.
 */
@Service
public class LevelConfigServiceImpl implements LevelConfigService {

    @Autowired
    private LevelConfigMapper levelConfigMapper;

    @Override
    public List<LevelConfig> list(Integer type) {
        checkNotNull(type, "类型不能为空");
        return levelConfigMapper.selectByLevelType(type);
    }

    @Override
    public List<LevelConfigVO> listFront(Integer type) {
        checkNotNull(type, "类型不能为空");
        List<LevelConfig> configs = levelConfigMapper.selectByLevelType(type);
        Collections.reverse(configs);
        List<LevelConfigVO> levelConfigVOS = new ArrayList<>();
        PeekingIterator<LevelConfig> iter = Iterators.peekingIterator(configs.iterator());
        while (iter.hasNext()) {
            LevelConfigVO current = new LevelConfigVO();
            BeanUtils.copyProperties(iter.next(), current);
            if (iter.hasNext()) {
                current.setNextValue(iter.peek().getLevelValue());
            }
            levelConfigVOS.add(current);
        }
        return levelConfigVOS;
    }

    @Override
    public void save(LevelConfigReq config) {
        LevelConfig conf = new LevelConfig();
        BeanUtils.copyProperties(config, conf);
        if (conf.getId() == null) {
            levelConfigMapper.insert(conf);
        } else {
            levelConfigMapper.updateByPrimaryKeySelective(conf);
        }

    }

    @Override
    public void delete(Integer id) {
        levelConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(LevelConfigReq config) {
        LevelConfig conf = new LevelConfig();
        BeanUtils.copyProperties(config, conf);
        levelConfigMapper.updateByPrimaryKeySelective(conf);
    }

    @Override
    public LevelConfig get(Integer id) {
        checkNotNull(id,  "id 不能为空");
        return levelConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, LevelConfig> find(LevelUser user) {
        int purchaseValue = 0;
        int providerValue = 0;
        if (user != null) {
            purchaseValue = user.getPurchaseValue();
            providerValue = user.getProviderValue();
        }
        // 查询集采等级
        LevelConfig purchaseConfig = levelConfigMapper.selectLevel(LevelTypeEnum.PURCHASE.getIndex(), purchaseValue);
        // 查询供应等级
        LevelConfig providerConfig = levelConfigMapper.selectLevel(LevelTypeEnum.PROVIDER.getIndex(), providerValue);
        Map<String, LevelConfig> configMap = new HashMap<>();
        configMap.put("purchase", purchaseConfig);
        configMap.put("provider", providerConfig);
        return configMap;
    }

    @Override
    public LevelConfig getLevel(Integer value, Integer type) {
        return levelConfigMapper.selectLevel(type, value);
    }
}
